package cn.chiship.bs.api.biz.controller;

import cn.chiship.bs.api.biz.entity.ProductTags;
import cn.chiship.bs.api.biz.entity.ProductTagsExample;
import cn.chiship.bs.api.biz.pojo.dto.ProductTagsDto;
import cn.chiship.bs.api.biz.pojo.dto.UpmsUserDto;
import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.bs.api.biz.entity.UpmsUser;
import cn.chiship.bs.api.biz.entity.UpmsUserExample;
import cn.chiship.bs.api.biz.service.BaseService;
import cn.chiship.bs.api.biz.service.UpmsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户控制器
 *
 * @author lj
 */
@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/user")
public class UpmsUserController extends BaseController<UpmsUser, UpmsUserExample> {
    @Resource
    UpmsUserService upmsUserService;

    @Override
    public BaseService<UpmsUser, UpmsUserExample> getService() {
        return upmsUserService;
    }

    @ApiOperation(value = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataTypeClass = String.class, paramType = "query"),
    })
    @Authorization
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(
            @RequestParam(required = false, value = "userName") String userName,
            @RequestParam(required = false, value = "realName") String realName,
            @RequestParam(required = false, value = "mobile") String mobile) {
        UpmsUserExample upmsUserExample = new UpmsUserExample();
        UpmsUserExample.Criteria criteria = upmsUserExample.createCriteria();
        if (!StringUtil.isNullOrEmpty(userName)) {
            criteria.andUserNameLike(userName + "%");
        }
        if (!StringUtil.isNullOrEmpty(realName)) {
            criteria.andRealNameLike(realName + "%");
        }
        if (!StringUtil.isNullOrEmpty(mobile)) {
            criteria.andMobileLike(mobile + "%");
        }
        return super.responseEntity(BaseResult.ok(super.page(upmsUserExample)));
    }


    @ApiOperation(value = "保存用户")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid UpmsUserDto upmsUserDto) {
        UpmsUser upmsUser = new UpmsUser();
        BeanUtils.copyProperties(upmsUserDto, upmsUser);
        return super.responseEntity(super.save(upmsUser));
    }

    @ApiOperation(value = "更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid UpmsUserDto upmsUserDto) {
        UpmsUser upmsUser = new UpmsUser();
        BeanUtils.copyProperties(upmsUserDto, upmsUser);
        return super.responseEntity(super.update(id, upmsUser));
    }

    @ApiOperation(value = "删除用户数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键ID集合,多个ID,请用,分割", required = true, dataType = "List", paramType = "body")
    })
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody List<String> ids) {
        UpmsUserExample upmsUserExample = new UpmsUserExample();
        upmsUserExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(upmsUserExample));

    }


}
